// pages/material/pages/attest/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: 0,
        type:'',//0委托书1声明书
        client_name:'',
        client_id_number:'',
        info_title: '房产过户交易认证', //当前认证的业务标题
        terms_info: [ //文书条款
            {
                id: 1,
                title: '《公证处公证申请表》',
                approved: false,
            },
            {
                id: 2,
                title: '《公证受理通知单回执》',
                approved: false,
            },
            {
                id: 3,
                title: '《涉外民事类公证告知书》',
                approved: false,
            },
            {
                id: 4,
                title: '《询问记录》',
                approved: false,
            },
            {
                id: 5,
                title: '《送达回证》',
                approved: false,
            },
        ],
        entrust_show: false, //是否显示委托书板块（出生、亲自 没有不显示  委托公证 显示 ）
        read_all: false, //是否全部阅读
        entrust: false, //是否阅读了委托书
        sign: '', //签名图片
        sign_src: '', //签名图片
        verified: false, //实名认证对比
        face_detect: false, //人脸识别 默认false
        isAgree: false, //相关协议是否同意
        checkbox_animate: false, //震动效果
    },
            //协议勾选
            checkboxChange: function (e) {
                var that = this;
               
                that.setData({
                    isAgree: !!e.detail.value.length
                })
                
                if(e.detail.value.length==1){
                    let arr = that.data.terms_info;
                    for (let i in arr) {
                        arr[i].approved = true;
                        that.setData({
                            terms_info: arr,
                        });
                    }
                    that.setData({
                        read_all: true
                    })
    
                }
                else{
                    let arr = that.data.terms_info;
                    for (let i in arr) {
                        arr[i].approved = false;
                        that.setData({
                            terms_info: arr,
                        });
                    }
                    that.setData({
                        read_all:false
                    })
                }
              
                
            },
    //跳转文书条款
    to_read: function (e) {
        console.log(e)
        let record_id = this.data.id
        let id = e.currentTarget.dataset.id; //对应条款id
        let index = e.currentTarget.dataset.index; //对应索引
        wx.navigateTo({
            url: '/pages/material/pages/terms/index?id=' + id + '&index=' + index + '&record_id=' + record_id,
        })
        setTimeout(() => {
            this.setData({
                // [`terms_info[${index}].approved`]: true,
            })
        }, 500)
    },
    //第2步  委托书阅读
    to_entrust: function (e) {
        console.log(e)
        let record_id = this.data.id
        let sign = 0
        wx.navigateTo({
            url: '/pages/material/pages/entrust/index?record_id=' + record_id + '&sign='+sign,
        })
    },
    //跳转签名
    to_sign: function (e) {
        wx.navigateTo({
            url: '/pages/material/pages/signature/index'
        })
    },
    //删除签名
    to_delete: function (e) {
        this.setData({
            sign: '',
            sign_src:''
        })
    },
    //查看图片
    previewImage: function (e) {
        var source = e.currentTarget.dataset.source;
        console.log(e)
        wx.previewImage({
            current: e.currentTarget.source, // 当前显示图片的http链接
            urls: [source] // 需要预览的图片http链接列表
        })
    },
    //实名认证跳转
    to_verified: function (e) {
        console.log('实名认证跳转')
        wx.navigateTo({
            url: '/pages/material/pages/verified/index',
        })
    },
    //人脸识别
    to_face: function (e) {
        console.log('人脸识别')
        let record_id = this.data.id
        
        //获取订单的委托人
        let  params = {
            id:record_id
        }
        const that = this;
        getApp()._post_form('data/api.auth.order/getorderusermsg', params, (result) => {
            that.setData({
                client_name: result.data.client_name,
                client_id_number: result.data.client_id_number,
            })
            //调用人脸识别接口
            wx.startFacialRecognitionVerify({
                name:that.data.client_name,
                idCardNumber:that.data.client_id_number,
                success(){
                    // 识别成功
                    that.setData({
                        face_detect: true,
                    })
                },
                fail(res){
                    // 识别失败
                    const verifyResult = res.verifyResult
                    console.log(res);
                    wx.showToast({
                        title: '识别失败',
                        icon: 'none',
                        mask: true,
                        duration: 1500
                    });
                }
            })
        })
        // wx.navigateTo({
        //     url: '/pages/material/pages/distinguish/index?record_id=' + record_id
        // })
    },
    //循环判断
    isCon: function (e) {
        var that = this;
        let arr = that.data.terms_info;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].approved == false) {
                that.setData({
                    read_all: false,
                    isAgree: false, //相关协议是否同意
                })
                return false;
            }
        }
        that.setData({
            read_all: true,
            isAgree: true, //相关协议是否同意
        })
    },
    //提交
    to_submit: function (e) {
        var that = this;
        let read_all = that.data.read_all //是否全部阅读
        let entrust_show = that.data.entrust_show //是否开启委托书
        let entrust = that.data.entrust //是否阅读了委托书
        let sign = that.data.sign_src //签名图片
        let verified = that.data.verified //实名认证对比
        let face_detect = that.data.face_detect //人脸识别
        console.log(read_all, entrust, sign, verified, face_detect)
        let isAgree = that.data.isAgree
        if (isAgree == false) {
            wx.showToast({
                title: '请勾选我已阅读并同意以上文书条款',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            that.setData({
                checkbox_animate: true
            })
            setTimeout(() => {
                that.setData({
                    checkbox_animate: false
                })
            }, 1500)
            return false
        }
       else if (read_all == false) {
            wx.showToast({
                title: '请阅读并确认全部文书条款',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            return false
        } else if (entrust_show == true && entrust == false) {
            wx.showToast({
                title: '请阅读并确认委托书',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            return false
        } else if (sign == '') {
            wx.showToast({
                title: '请完成手写签字',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            return false
        } 

        // 暂时跳过
        // else if (verified == false) {
        //     wx.showToast({
        //         title: '请进行实名认证对比',
        //         icon: 'none',
        //         mask: true,
        //         duration: 1500
        //     });
        //     return false
        // } 
        else if (face_detect == false) {
            wx.showToast({
                title: '请进行人脸识别',
                icon: 'none',
                mask: true,
                duration: 1500
            });
            return false
        }
        that.setData({
            disabled: true
        })
        wx.showToast({
            title: '上传中...',
            icon: 'loading',
            mask: true,
            duration: 1500,
            success(res) {
                let params = {}
                params.id = that.data.id;
                params.sign_img = that.data.sign_src;
                console.log(params);
                getApp()._post_form('data/api.auth.order/authentication', params, (result) => {
                    // let rt_data = result.data
                    // console.log(rt_data);
                    setTimeout(() => { //上传成功后
                        wx.reLaunch({
                            url: '/pages/material/pages/approval_status/index?id='+that.data.id,
                        })
                    }, 1500)
                })
                
            }
        });

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options.base_type)
        console.log(options,'2222')
        if (options.base_type === '0') {
            this.setData({
                entrust_show: true
            })
        } else {
            this.setData({
                entrust_show: false
            })
        }
        // 记录id
        this.setData({
            id: options.id
        })
          // 契约锁url
          this.setData({
            qys_ticket: decodeURIComponent(options.qys_ticket)
        })
        //获取声明书还是委托书
        // this.getProxy_type();
        // this.getItem();
    },
    //获取认证流程
    getItem:function(){
        let params = {}
        params.mark= '认证流程';
        getApp()._post_form('data/api.news/getItemfive', params, (result) => {
            this.setData({
                terms_info: result.data,
            });
        })
    },
    getProxy_type:function(){
        let params = {}
        params.id= this.data.id;
        getApp()._post_form('data/api.auth.order/getorderusermsg', params, (result) => {
            this.setData({
                type: result.data.proxy_type,
            });
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // this.isCon();
        console.log('onShow2222')
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
})